Page({
    data: {
        params: {
            pageNum: 1,
            pageSize: 9999
        },

        // 地址列表

        locationList: [],

        bookInfoIds: '',

        shipType: '',

        shippingAddressId: ''
    },
    //添加新地址
    addDizhi () {
        wx.navigateTo({
            url: '/package/juanshu/pages/addDizhi/addDizhi',
        })
    },

    //确认支付

    paySuccess () {


      if (this.data.locationList.length == 0) {
        wx.showToast({
          title: '请添加地址',
          icon: 'none'
        })
        return
      }

      var params = {
        ids: this.data.shippingAddressId,
        name: this.data.name,
        userTel: this.data.userTel,
        province: this.data.province,
        city: this.data.city,
        county: this.data.county,
        detailAddress: this.data.detailAddress
      }
      var pages = getCurrentPages();

      var prevPage = pages[pages.length - 2];

      prevPage.addDizhi2(params)



        wx.navigateBack()



        // var params = {
        //     bookInfoIds: this.data.bookInfoIds,
        //     shipType: this.data.shipType,
        //     shippingAddressId: this.data.shippingAddressId
        // }

        // this.crateOrder(params)
        // // wx.navigateTo({
        // //   url: '/package/juanshu/pages/paySuccess/paySuccess',
        // // })
    },


    //创建订单接口

    crateOrder (params) {
        wx.request({
            url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/donate/donate/create/auth',
            data: params,
            method: 'POST',
            header: {
                "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
              "token" : wx.getStorageSync('token'),
                "uid" : wx.getStorageSync('uuid')
            },
            success: (res) =>  {
                console.log(res)

                if (res.data.code == 200) {
                    wx.showToast({
                        title: '创建成功',
                        icon: 'none',
                        duration: 1000
                    })

                    setTimeout(function(){
                        wx.navigateTo({
                            url: '/package/juanshu/pages/paySuccess/paySuccess',
                        })
                    },1000)
                }else{
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            }
        })
    },


    // 接口部分

    //地址列表
    getLocalList(params) {

        var _this = this
        wx.request({
            url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/address/list/auth',
            data: params,
            method: 'POST',
            header: {
                "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
              "token" : wx.getStorageSync('token'),
                "uid" : wx.getStorageSync('uuid')
            },
            success: (res) => {
                console.log(res)

                if (res.data.code == 200) {

                    for (var i = 0; i < res.data.data.resultList.length; i++) {
                        if (i==0) {
                            res.data.data.resultList[i].ischoose = true
                            _this.setData({
                                shippingAddressId: res.data.data.resultList[i].id,

                              name: res.data.data.resultList[i].name,
                              userTel: res.data.data.resultList[i].userTel,
                              province: res.data.data.resultList[i].province,
                              city: res.data.data.resultList[i].city,
                              county: res.data.data.resultList[i].county,
                              detailAddress: res.data.data.resultList[i].detailAddress
                            })
                        }else{
                            res.data.data.resultList[i].ischoose = false
                        }
                    }

                    var list = res.data.data.resultList

                    _this.setData({
                        locationList: list
                    })
                }else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            }
        })
    },

    //删除地址
    shanchu(e) {

        var _this = this
        console.log(e.currentTarget.dataset.localid)
        var localid = e.currentTarget.dataset.localid
        wx.showModal({
            title: '删除地址',
            content: '确定删除该地址',
            success: function (res) {
                if (res.cancel) {
                    return false
                }else{
                    _this.shanchuApi(localid)
                }
            }
        })
    },

    shanchuApi (id) {
        var _this = this
        var params = {
            ids: id
        }
        wx.request({
            url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/address/del/auth',
            data: params,
            method: 'POST',
            header: {
                "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
              "token" : wx.getStorageSync('token'),
                "uid" : wx.getStorageSync('uuid')
            },
            success: (res) => {
                console.log(res)

                if (res.data.code==200) {
                    wx.showToast({
                        title: '删除成功',
                        icon: 'none'
                    })

                  // for (var i = 0; i < _this.data.locationList.length;i++) {
                  //   if (id == ) {

                  //   }
                  //  }
                    _this.getLocalList(_this.data.params)

                  var pages = getCurrentPages();

                  var prevPage = pages[pages.length - 2];

                  prevPage.onLoad(prevPage.options)

                }else{
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                }
            }
        })
    },


    // 选择地址

    choooseLocal (e) {

        console.log(e.currentTarget.dataset.localids)

        var ids = e.currentTarget.dataset.localids

        for (var i = 0; i < this.data.locationList.length;i++) {
            var ischoose = "locationList[" + i + "].ischoose"
            if (ids == this.data.locationList[i].id) {
                this.setData({
                    [ischoose]: true,
                    shippingAddressId: ids,
                    name: this.data.locationList[i].name,
                    userTel: this.data.locationList[i].userTel,
                  province: this.data.locationList[i].province,
                  city: this.data.locationList[i].city,
                  county: this.data.locationList[i].county,
                  detailAddress: this.data.locationList[i].detailAddress
                })
            }else{
                this.setData({
                    [ischoose]: false
                })
            }
        }
    },


    onLoad (options) {
        console.log(options.bookInfoIds)

      if (options.bookInfoIds && options.shipType) {
        this.setData({
            bookInfoIds: options.bookInfoIds,
            shipType: options.shipType
        })
      }
    },
    onShow () {
        this.getLocalList(this.data.params)
    }
})
